// pages/find/add-comment/comment.jss
const T = require('../../../utils/tool.js')
const config = require('../../../config.js')
let token = ''
let userId = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList: [],
    uploadUrl: config.host + 'admin/file/singleFile',
    formData: {
      type: 'image'
    },
    lord: 0, // 是否楼主
    topicId: -1,
    commentId: -1,
    toUserId: -1,
    comment: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    token = getApp().globalData.token;
    userId = getApp().globalData.userId;
    let {
      topicId,
      lordid,
      commentId,
      lord,
    } = options;
    this.data.topicId = topicId;
    this.data.commentId = commentId;
    this.data.lordid = lordid;
    this.data.lord = lord;
    this.setData({ lord: Number(lord) });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  onUploadSuccess(e) {
    console.log(e);
    this.setData({
      fileList: e.detail.fileList.map((n) => {
        // 如果上传失败显示红框
        if (n.status == 'error') {
          return n
        }
        let _resData = JSON.parse(n.res.data);
        if (_resData.code != 200) {
          return Object.assign(n, {
            status: 'error'
          });
        } else {
          return Object.assign(n, {
            status: 'success'
          });
        }
      })
    })
  },

  /**
   * 移除上传失败的图片
   */
  onRemoveImg(e) {
    this.data.fileList.splice(e.detail.index, 1);
  },

  onUploadFail(e) {
    console.log(e)
  },

  onComment(e) {
    this.setData({
      comment: e.detail.value,
    });
  },

  onPublish() {
    let topic = {};
    let fileList = this.data.fileList,
      comment = this.data.comment;

    topic.topicId = Number(this.data.topicId);
    topic.userId = userId;
    topic.replyCommentId = Number(this.data.commentId);
    topic.commentId = Number(this.data.lordid);
    topic.content = comment;

    let isUploadFail = false;
    let photoIndex = 0;
    fileList.forEach((ele, i) => {
      let _resData = JSON.parse(ele.res.data);
      photoIndex = (i + 1);
      if (!_resData.err) {
        topic['photo' + (i + 1)] = _resData.data.url;
      } else {
        isUploadFail = true;
      }
    });
    photoIndex = photoIndex == 0 ? 0 : photoIndex;

    if (!comment && fileList.length == 0) {
      T.toast({
        id: '#wux-toast',
        text: '请输入评论内容或者上传一张图片',
        success: () => { }
      })
      return;
    }


    for (let i = photoIndex + 1; i < 10; i++) {
      topic['photo' + i] = '';
    }
    if (isUploadFail) {
      T.toast({
        id: '#wux-toast',
        text: '有某张图片上传失败,请重新上传',
        success: () => { }
      })
      return;
    }

    if (this.data.lord == 0) { // 回复评论 
      this.onReplay(topic);
    } else { // 发表评论
      this.onPublishComment(topic)
    }


  },

  onReplay(topic) {
    T.req({
      loadTitle: '请求中....',
      url: config.api + 'forum/topic/reply?token=' + token,
      method: 'POST',
      data: {
        topicId: topic.topicId,
        content: topic.content,
        userId: userId,
        replyCommentId: topic.replyCommentId,
        commentId: topic.commentId
      },
      success: (res) => {
        wx.showToast({
          title: '发表成功',
          duration: 2000,
          success: () => {
            setTimeout(function () {
              wx.navigateBack({
                detail: 1
              })
            }, 1500)
          }
        })
      }
    })
  },

  onPublishComment(topic) {
    T.req({
      loadTitle: '请求中....',
      url: config.api + 'forum/topic/comment?token=' + token,
      method: 'POST',
      data: topic,
      success: (res) => {
        wx.showToast({
          title: '发表成功',
          duration: 2000,
          success: () => {
            setTimeout(function () {
              wx.navigateBack({
                detail: 1
              })
            }, 1500)
          }
        })
      }
    })
  }
})